package com.hm.pan.controller;

import java.sql.Timestamp;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hm.pan.annotation.CheckLogin;
import com.hm.pan.model.ResultObj;
import com.hm.pan.model.UserFile;
import com.hm.pan.service.MoveFileService;
import com.hm.pan.service.UserFileService;

@RestController
@RequestMapping("userfile")
@CheckLogin
public class MoveFileController {
	@Resource
	private MoveFileService moveFileService;
	@Resource
	private UserFileService userFileService;

	// 剪切粘贴
	@RequestMapping("cutFile")
	public Object cutFile(String target, Integer userfileId) throws Exception {

		UserFile userFile = userFileService.getOneFileByPath(target);
		Long fatherId = userFile.getUserfileId();

		System.out.println("+++++++++" + userfileId);
		System.out.println(fatherId);

		Boolean cutFile = moveFileService.cutFile(fatherId, userfileId);
		System.out.println(cutFile);

		return new ResultObj(200, cutFile);
	}

	// 复制粘贴文件
	@PostMapping("copyFIle")
	public Object copyFile(@RequestParam("target") String target, @RequestParam("userfileId") Integer userfileId)
			throws Exception {
		ResultObj resultObj = new ResultObj();
		UserFile fatherFile = userFileService.getOneFileByPath(target); // 获取目标文件的信息
		UserFile oneFile = moveFileService.getOneFile(userfileId); // 获取userFile的文件信息
		Timestamp newTime = new Timestamp(System.currentTimeMillis()); // 获取系统当前时间

		UserFile userFile = new UserFile();
		userFile.setFileId(oneFile.getFileId()); // 获取文件ID并添加
		userFile.setFileType(oneFile.getFileType()); // 获取文件类型并添加
		userFile.setFileName(oneFile.getFileName()); // 获取文件名称并添加
		userFile.setUserId(oneFile.getUserId()); // 获取用户ID并添加
		userFile.setUserId(oneFile.getUserId()); // 获取用户ID并添加
		userFile.setUploadTime(newTime); // 添加修改时间
		userFile.setFatherId(fatherFile.getUserfileId()); // 添加父文件ID并添加
		if (moveFileService.copyFile(userFile)) { // 创建一个新文件,判断是否添加成功
			resultObj.setMessage("success");
		} else {
			resultObj.setMessage("fails");
		}
		return resultObj;
	}

}
